home *** CD-ROM | disk | FTP | other *** search
/ Internet Info 1994 March / Internet Info CD-ROM (Walnut Creek) (March 1994).iso / networking / mail / mh / contrib / 9302 / mhpped.shar / mhpped next >
Text File  |  1993-02-03  |  5KB  |  214 lines

  1. #!/usr/bin/perl
  2. #
  3. # mhpped - process backticks in comp, repl, and forw components
  4. #
  5. # Taken from "testedit" by Jerry Peek <jerry@ora.com>;
  6. # abuser: Jerry Sweet <jsweet@irvine.com>
  7. #
  8. # $Header: /usr/t/users/jsweet/src/mh-front/RCS/mhpped,v 1.5 1992/11/12 10:03:47 jsweet Exp $
  9.  
  10. #
  11. # Subprograms
  12. #
  13.  
  14. sub bail {
  15.   unlink $temp;
  16.   exit 1;
  17. }
  18.  
  19. sub gripe {
  20.   print STDERR "$prog: ", @_, "\n";
  21. }
  22.  
  23. sub barf {
  24.   &gripe(@_);
  25.   &bail;
  26. }
  27.  
  28. sub check {
  29.   local($status) = @_;
  30.   &barf("problem writing to temporary draft file \"$temp\"- $!")
  31.     unless $status;
  32. }
  33.  
  34. sub sh {
  35.   local($_) = @_;
  36.   local($st);
  37.  
  38.   if ($verbose) {
  39.     print "\t$_\n";
  40.   }
  41.  
  42.   return if $debug;
  43.  
  44.   $st = system "$_";
  45.  
  46.   if ($st >> 8) {
  47.     split;
  48.     &barf("$_[0] failed with status ", $st >> 8);
  49.     exit 1;
  50.   }
  51. }
  52.  
  53. sub read_profile {
  54.   # Defines associative array %PROFILE_COMPONENT indexed by lower case
  55.   # component name.  Continuation lines are tacked on.  New
  56.   # occurrences of a component wipe out previous occurrences of the
  57.   # same component, but probably shouldn't.
  58.  
  59.   local($profile) = defined $ENV{'MH'} ? $ENV{'MH'} : 
  60.                      $ENV{'HOME'} . "/.mh_profile";
  61.   local($profile_component) = 'x-bogus';
  62.   local($_);
  63.   
  64.   &barf("can't open profile \"$profile\" - $!")
  65.     unless open(PROFILE, "<$profile");
  66.  
  67.   while (<PROFILE>) {
  68.     /^(\S+):\s*/ && do {
  69.             ($profile_component = $1) =~ tr/A-Z/a-z/;
  70.             $PROFILE_COMPONENT{$profile_component} = $';
  71.         };
  72.   
  73.     /^[ \t]/ && do {
  74.             $PROFILE_COMPONENT{$profile_component} .= $_;
  75.         };
  76.   }
  77. }
  78.  
  79. #
  80. # Main
  81. #
  82.  
  83. umask 077;  # This should probably be adjustable, but isn't at the moment.
  84.  
  85. ($prog = $0) =~ s|.*/||;    # Get the invocation name of this program
  86.  
  87. # Check the profile
  88.  
  89. &read_profile;
  90. if (defined $PROFILE_COMPONENT{$prog}) {
  91.   push(@Args, split(' ', $PROFILE_COMPONENT{$prog}));
  92. }
  93.  
  94. # Check the command line arguments
  95.  
  96. push(@Args, @ARGV);
  97.  
  98. while ($_ = shift @Args) {
  99.   /^-debug$/ && do { ++$debug; next; };
  100.  
  101.   /^-editor$|^-edit$/ && do { $editor = shift @Args; next; };
  102.  
  103.   /^-entire$/ && do { ++$entire; next; };
  104.  
  105.   /^-help$/ && do {
  106.       print <<"xxEndHelpxx";
  107. $prog - pre-process an MH composition template
  108. Usage: $prog [-(no)debug] [-editor editor] [-noeditor] [-(no)entire] [-help]
  109.                 [-(no)verbose] [file]
  110. Options:
  111.   -debug          print the message draft and invoke default whatnow
  112.   -editor editor  invoke the specified editor following processing
  113.   -entire         pre-process the entire message, including the body
  114.   -help           print this message
  115.   -nodebug        negates the -debug option
  116.   -noeditor       disables automatic invocation of an editor
  117.   -noentire       negates the -entire option; only the header is processed
  118.   -noverbose      negates the -verbose option
  119.   -verbose        print what's going on as it's being done
  120.   file            if omitted, envar \$mhdraft names the draft file
  121. xxEndHelpxx
  122.  
  123.       exit 1;
  124.     };
  125.  
  126.   /^-nodebug$/ && do { --$debug; next; };
  127.   /^-noeditor$|^-noedit$/ && do { $editor = ''; next; };
  128.   /^-noentire$/ && do { --$entire; next; };
  129.   /^-noverbose$/ && do { --$verbose; next; };
  130.  
  131.   /^-verbose$/ && do { ++$verbose; next; };
  132.  
  133.   /^[^\-]/ && do { $file = $_; next; };
  134.  
  135.   &barf("unrecognized command line option \"$_\"");
  136. }
  137.  
  138. $debug = $debug > 0;
  139. $entire = $entire > 0;
  140. $verbose = $verbose > 0;
  141.  
  142. # Do some initial setup.
  143.  
  144. $temp = "/tmp/$prog.$$";
  145. $SIG{'HUP'} = $SIG{'INT'} = $SIG{'TERM'} = 'bail';
  146.  
  147. # Figure out the name of the draft file
  148.  
  149. if (! $file) {
  150.   if (defined $ENV{'mhdraft'}) {
  151.     $file = $ENV{'mhdraft'};
  152.   }
  153.   else {
  154.     &barf("no draft file specified");
  155.   }
  156. }
  157. else {
  158.   $ENV{'mhdraft'} = $file;
  159. }
  160.  
  161. # Let's start running our dirty fingers over the draft file...
  162.  
  163. &barf("can't open draft file \"$file\" - $!") 
  164.     unless open(DRAFT, "<$file");
  165. &barf("can't create temporary draft file \"$temp\" - $!")
  166.     unless open(TEMP, ">$temp");
  167.  
  168. while (<DRAFT>) {
  169.   last if (/^$|^-/ && ! $entire);
  170.  
  171.   if (/\`(.+)\`/) {
  172.     $torun = $1;
  173.     $prefix = $`;
  174.     $suffix = $';
  175.  
  176.     print("\t$torun\n") if ($verbose || $debug);
  177.     chop($result = `$torun`);
  178.     if ($? >> 8) {
  179.       &gripe("$file: line $.: command \"$torun\" returned error status ",
  180.         $? >> 8);
  181.     }
  182.     next if ($prefix =~ /^\s*$|^\S+:\s*$/) && 
  183.         ($suffix =~ /^\s*$/) && 
  184.         ! $result;
  185.         # If there was no result, and there was nothing important
  186.         # before or after the backtick construct, then don't even
  187.         # bother printing a line.  This allows us to omit
  188.         # header components programmatically.
  189.  
  190.     &check(print(TEMP $prefix, $result, $suffix));
  191.   }
  192.   else {
  193.     &check(print TEMP $_);
  194.   }
  195. }
  196.  
  197. if (! $entire) {
  198.   &check(print TEMP "-------\n");
  199.   grep(&check(print TEMP $_) && 0, <DRAFT>);
  200. }
  201.  
  202. &barf("problem closing temporary draft file \"$temp\" - $!")
  203.     unless close(TEMP);
  204.  
  205. close(DRAFT);
  206.  
  207. &sh("cp $temp $file");
  208. system('/bin/cat', $temp) if $debug;
  209.  
  210. print("\trm $temp\n") if ($verbose || $debug);
  211. unlink $temp;
  212.  
  213. &sh("$editor $file") if $editor;
  214.